﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Drawing;
using System.Threading;
using Google.Protobuf.WellKnownTypes;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 发货日计划_提交后分配数量更新
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CRM_ReDPlanQtyController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string gz_con;


        public CRM_ReDPlanQtyController(ILogger<CRM_ReDPlanRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            _logger = loggerFactory.CreateLogger<CRM_ReDPlanQtyController>();

        }

        /// <summary>
        /// 发货日计划_提交后分配数量更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostReDPlanQty([FromBody] CRM_ReDPlanRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            CRM_ReDPlanResponseDto re = new CRM_ReDPlanResponseDto();


            if (strparm.data ==null)
            {

                strMsg = "明细不能为空！";
            }

            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            try
            {
                if (strMsg == "")
                {
                    int i = 1;
                    foreach (var item in strparm.data)
                    {
                        string sysid = "";

                        if (item.data_type=="成品组件")
                        {
                            sysid = "TMS0188";
                        }
                        else
                        {
                            sysid = "TMS0194";
                        }
                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("shipment_rowid", item.shipment_rowid);
                        string strSQL = MethodHelp.GetTranSQL(sysid, dic, con);//更新已分配数量
                        if (strSQL.Trim() != "")
                        {
                            strSqlList.Add(strSQL.ToString());
                        }
                        else
                        {
                            strMsg = "Parameter conversion failed";
                        }

                    }
                    if (strMsg==""&& strSqlList.Count>0)
                    {
                        bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                        if (A)
                        {

                        }
                        else
                        {
                            strMsg = "TMS0188 Database update failed";
                        }
                    }
                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
